import pymysql
import pandas as pd

mydb = pymysql.connect(
    host="rm-2zea30h4sh8g15zd1ho.mysql.rds.aliyuncs.com",  # MySQL host
    port=3306,  # MySQL port
    user="root",  # MySQL username
    password="Ds2024@()833429",  # MySQL password
    database="douyinpinglun",
)
cursor = mydb.cursor()
# 执行查询以获取表数据
query = "SELECT * FROM 抖音视频表 order by id"
cursor.execute(query)

# 获取查询结果
result = cursor.fetchall()

# 获取列名
columns = [desc[0] for desc in cursor.description]

# 将结果转换为 DataFrame
df = pd.DataFrame(result, columns=columns)

# 将 DataFrame 导出为 Excel 文件
output_file = "抖音视频表_data.xlsx"
df.to_excel(output_file, index=False, engine='openpyxl')

# 关闭数据库连接
cursor.close()
mydb.close()

print(f"数据已成功导出到 {output_file}")